package com.example.hasee.zhcd.Adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hasee.zhcd.Activities.ChuanWu.ArrivalsDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.NearDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.NearEntrustActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.NearPointActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.ShipDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.WayBillActivity;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;

import java.util.ArrayList;
import java.util.HashMap;

import static com.example.hasee.zhcd.R.id.fzygs;

/**
 * Created by hasee on 2017/9/20.
 */

public class NearListAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, String>> list;
    private LayoutInflater itemInflater;
    private Context context;
    private Holder holder;

    public NearListAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        this.context = context;
        this.list = list;
        this.itemInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new Holder();
            convertView = itemInflater.inflate(R.layout.near_list_item, null);
            holder.fstatus = (ImageView) convertView.findViewById(R.id.fstatus);
            holder.fshipname = (TextView) convertView.findViewById(R.id.fshipname);
            holder.fberth = (TextView) convertView.findViewById(R.id.fberth);
            holder.fzd = (TextView) convertView.findViewById(R.id.fzd);
            holder.fwbsx = (TextView) convertView.findViewById(R.id.fwbsx);
            holder.fzygs = (TextView) convertView.findViewById(R.id.fzygs);
            holder.fkbsj = (TextView) convertView.findViewById(R.id.fkbsj);
            holder.detail = (TextView) convertView.findViewById(R.id.detail);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if ("预靠泊".equals(list.get(position).get("fstatus"))) {
            holder.fstatus.setImageResource(R.mipmap.yukaobo);
        } else if ("靠泊".equals(list.get(position).get("fstatus"))) {
            holder.fstatus.setImageResource(R.mipmap.kaobo);
        } else if ("正在离港".equals(list.get(position).get("fstatus"))) {
            holder.fstatus.setImageResource(R.mipmap.ligang);
        }

        holder.fshipname.setText((String) list.get(position).get("fshipname"));
        holder.fshipname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShipDetailActivity.class);
                intent.putExtra("strSYN", list.get(position).get("fsyn"));
                context.startActivity(intent);
            }
        });
        holder.fshipname.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        holder.fberth.setText((String) list.get(position).get("fberth"));
        holder.fkbsj.setText((String) list.get(position).get("fkbsj"));
        if (list.get(position).get("fzd").toString().trim().length() == 0) {
            holder.fzd.setVisibility(View.INVISIBLE);
        } else {
            holder.fzd.setVisibility(View.VISIBLE);
            holder.fzd.setText((String) list.get(position).get("fzd") + ">>");
            holder.fzd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, NearPointActivity.class);
                    intent.putExtra("strShipCode", list.get(position).get("fshipcode"));
                    intent.putExtra("strSYN", list.get(position).get("fsyn"));
                    intent.putExtra("strWTF", list.get(position).get("fwtf"));
                    context.startActivity(intent);
                }
            });
        }
        if (list.get(position).get("fwbsx").toString().trim().length() == 0) {
            holder.fwbsx.setVisibility(View.INVISIBLE);
        } else {
            holder.fwbsx.setVisibility(View.VISIBLE);
            holder.fwbsx.setText((String) list.get(position).get("fwbsx") + ">>");
            holder.fwbsx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, NearEntrustActivity.class);
                    intent.putExtra("strShipCode", list.get(position).get("fshipcode"));
                    intent.putExtra("strSYN", list.get(position).get("fsyn"));
                    context.startActivity(intent);
                }
            });
        }
        holder.detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NearDetailActivity.class);
                intent.putExtra("strSyn", list.get(position).get("fsyn"));
                context.startActivity(intent);
            }
        });
        if (list.get(position).get("fzygs").length() == 0) {
            holder.fzygs.setText(list.get(position).get("fzygs"));
        } else {
            holder.fzygs.setText(list.get(position).get("fzygs"));
            holder.fzygs.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            holder.fzygs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, WayBillActivity.class);
                    intent.putExtra("strSYN", list.get(position).get("fsyn"));
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    class Holder {
        private ImageView fstatus;
        private TextView fshipname;
        private TextView fberth;
        private TextView fzd;
        private TextView fwbsx;
        private TextView fzygs;
        private TextView fkbsj;
        private TextView detail;
    }
}
